package com.zhoux.api.io.raf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author taoyi
 * @version 1.0
 * @date 2021/3/24 17:29
 *
 * 本类用于测试文件的复制操作
 *
 */
public class RAFDemo3 {
    public static void main(String[] args) {
        RandomAccessFile r = null;
        RandomAccessFile w = null;
        File file = new File(".\\src\\main\\java\\com\\zhoux\\file\\11.gif");
        File file2 = new File(".\\src\\main\\java\\com\\zhoux\\file\\22.gif");
        try {
            r = new RandomAccessFile(file,"r");
            w = new RandomAccessFile(file2,"rw");
            int d;
            long start = System.currentTimeMillis(); // 获取当前系统时间的毫秒值
            try {
                while ((d=r.read())!=-1) {
                    w.write(d); // 向文件中写入一个字节, 写入的是给定的 int 值所对应的二进制的“低八位”
                }
                long end = System.currentTimeMillis();
                System.out.println("复制完成！耗时: "+(end-start)+"ms");
            } catch (IOException e) {
                System.out.println("写入失败！");
                e.printStackTrace();
            }
            System.out.println("执行完毕！");
        } catch (FileNotFoundException e) {
            System.out.println("执行失败！");
            e.printStackTrace();
        }finally {
            try {
                w.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                r.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

